{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jupyter environment detected. Enabling Open3D WebVisualizer.\n",
      "[Open3D INFO] WebRTC GUI backend enabled.\n",
      "[Open3D INFO] WebRTCWindowSystem: HTTP handshake server disabled.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import open3d as o3d\n",
    "import time\n",
    "\n",
    "# 读取深度图像\n",
    "depth_img = cv2.imread(\"../img/1_depth.png\", cv2.IMREAD_GRAYSCALE)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "depth_img #0-255？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 假设你的深度图存储在名为depth的numpy数组中\n",
    "# depth_img = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])\n",
    "\n",
    "# 将深度图转换为uint16类型\n",
    "depth_uint16 = depth_img.astype(np.uint16)\n",
    "\n",
    "# 使用转换后的深度图初始化Image对象\n",
    "depth_o3d = o3d.geometry.Image(depth_uint16)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time elapsed average 10 times:0.0031 seconds.\n"
     ]
    }
   ],
   "source": [
    "# 十次求平均\n",
    "# # 内参矩阵不一样，关系不大\n",
    "# k = np.array([[535.4, 0.0, 320.0],\n",
    "#               [0.0, 539.2, 240.0],\n",
    "#               [0.0, 0.0, 1.0]])\n",
    "\n",
    "elapsed_time_list = []\n",
    "for i in range(10):# \n",
    "    start_time = time.time()  # 记录开始时间\n",
    "\n",
    "    # 将深度图转换为点云\n",
    "    point_cloud = o3d.geometry.PointCloud.create_from_depth_image(depth_o3d,\n",
    "                                                                o3d.camera.PinholeCameraIntrinsic(640, 480, 572.4114, 573.5704, 325.2611, 242.0489))\n",
    "\n",
    "    end_time = time.time()  # 记录结束时间\n",
    "\n",
    "    elapsed_time = end_time - start_time  # 计算耗时\n",
    "\n",
    "    elapsed_time_list.append(elapsed_time)\n",
    "\n",
    "average_time = sum(elapsed_time_list) / len(elapsed_time_list)\n",
    "\n",
    "print(f\"Time elapsed average 10 times:{average_time:.4f} seconds.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化点云\n",
    "\n",
    "# 点云数据存储在名为point_cloud的numpy数组中\n",
    "cloud = o3d.geometry.PointCloud()\n",
    "cloud.points = o3d.utility.Vector3dVector(point_cloud.points)\n",
    "\n",
    "# 创建一个可视化窗口\n",
    "vis = o3d.visualization.Visualizer()\n",
    "vis.create_window()\n",
    "\n",
    "# 将点云添加到可视化窗口中\n",
    "vis.add_geometry(cloud)\n",
    "\n",
    "# 设置相机位置，没调所以可有可无\n",
    "vis.get_view_control().set_front([0, 0, -1])\n",
    "vis.get_view_control().set_lookat([0, 0, 0])\n",
    "vis.get_view_control().set_up([0, -1, 0])\n",
    "\n",
    "# 开始显示点云\n",
    "vis.run()\n",
    "\n",
    "# 关闭显示窗口\n",
    "vis.destroy_window()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "0.003s"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env4cv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
